﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;

namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Récupération d'un WorkItem
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "ChangesetByWI")]
    public class Get_ChangesetByWI : Cmdlet
    {
        public Get_ChangesetByWI()
        {

            this.WIs = new List<WorkItem>();
            
        }
        protected override void ProcessRecord()
        {
            try
            {
                List<Changeset> retour = new List<Changeset>();
                if (this.WIs.Count > 0)
                {
                    foreach (WorkItem w in this.WIs)
                    {
                        retour.AddRange(w.ListChangeset());
                    }
                }
                else
                {
                    retour = this.WI.ListChangeset();
                }
                WriteObject(retour, true);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(ex.StackTrace);
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Get-WI is end");
            base.EndProcessing();
        }

        [Parameter(Position = 0)]
        [ValidateNotNullOrEmpty]
        public WorkItem WI { get; set; }


        [Parameter(Position = 0)]
        [ValidateNotNullOrEmpty]
        public List<WorkItem> WIs { get; set; }

    }
}